package br.com.felix.fwt.ui;

import java.io.PrintWriter;

import br.com.felix.fwt.ui.exception.ComponentRenderException;

/**
 * The Class HorizontalPanel is used to align components horizontally.
 */
public class HorizontalPanel extends BaseComponent{

	private static final long serialVersionUID = -5870936147989623320L;

	private int[] dividerLocation = null;
	/**
	 * Instantiates a new horizontal panel.
	 */
	public HorizontalPanel(){
		this.cssClass = "fwthorizontalpanel";
	}
	
	
	/* (non-Javadoc)
	 * @see br.com.felix.fwt.ui.BaseComponent#write(java.io.PrintWriter)
	 */
	@Override
	public void write(PrintWriter out) throws ComponentRenderException {
		if(!authorized()) return;
		if(childs!=null){
			out.write("\n<table class=\"");
			out.write(cssClass);
			out.write("\" ");
			if(style!=null){
				out.write(style);
			}
			out.write(">\n<tr>");
			if(dividerLocation==null){
				for(BaseComponent child:childs){
					out.write("\n<td>");
					child.write(out);
					out.write("\n</td>");
				}
			}
			else{
				int i=0;
				for(BaseComponent child:childs){
					out.write("\n<td ");
					if(i < dividerLocation.length){
						out.write("width=\"");
						out.write(dividerLocation[i]+"\"");
					}
					out.write(">");
					child.write(out);
					out.write("\n</td>");
				}
			}
			out.write("\n</tr>\n</table>");			
		}
	}


	public void setDivider(int... dividerLocation) {
		this.dividerLocation = dividerLocation;
	}

}
